﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DoodJetView.VuesPartielles;
using Doo_JetDAL;
using System.Data.Objects;
using DoodJetView.VuesPartielles.Reservation;

namespace DoodJetView.Core.Reservation
{
    /// <summary>
    /// Logique d'interaction pour PartialDayOfActivities.xaml
    /// </summary>
    public partial class PartialDayOfActivities : UserControl
    {
        private string p;
        private ReservationCreation reservationCreation;

        public PartialDayOfActivities(string title, ReservationCreation reservationCreation)
        {
            InitializeComponent();
            Lbl_Titre.Content = title;
            this.reservationCreation = reservationCreation;
            this.DynamicGrid = new DayOfActivities(DayGranul, 12, 480, this.ReservationView);
            
            remplileslabels();

        }

        public DayOfActivities DynamicGrid { get; set; }
      /*  public void RempliLagrille(int Duree, int idClient, int Activites, DateTime dt)
        {
            this.DynamicGrid.RempliLagrille(Duree, idClient, Activites,  dt);
        }*/
        private void remplileslabels() {
            Labels.Children.Clear();
            foreach (Label lb in DynamicGrid.RemplirLaGrilleLabel())
            {
                Labels.Children.Add(lb);
            }
        }

        internal void RempliLagrille(PartialDayOfActivities ret, int Duree, int idClient, int Activites, DateTime dt)
        {
            //this.DynamicGrid.RempliLagrille(ret, Duree, idClient, Activites, dt);
        }

        public ReservationCreation ReservationView { get; set; }
    }
    public delegate bool isReserved(int IdEquipement, int Datedeb);
    public class DayOfActivities 
    {
        private float NbHeuresTravaille;
        private int HeureDemareAenMin;
        private int granularite = 10;
        private DockPanel DayGranul;
        private int p;
        private int p_2;
        private ReservationCreation reservationCreation;
        public DayOfActivities( float NbHeureDsJournee, int HeureDemareAenMin, ReservationCreation reservation)
        {
            this.reservationCreation = reservation;
            // TODO: Complete member initialization
            this.NbHeuresTravaille = NbHeureDsJournee;
            this.HeureDemareAenMin = HeureDemareAenMin;
            this.DynamicGrid = new DynamicGridTools(650, false);
            this.CreateNbOfButton();
            RemplirLaGrille();

        }

    /*    public DayOfActivities(DockPanel partialDayOfActivities, float NbHeureDsJournee, int HeureDemareAenMin)
        {
            this.NbHeuresTravaille = NbHeureDsJournee;
            this.HeureDemareAenMin = HeureDemareAenMin;

            this.DynamicGrid = new DynamicGridTools(partialDayOfActivities.Width, false);
            this.View = partialDayOfActivities;
            
        }*/

        public DayOfActivities(DockPanel partialDayOfActivities, float NbHeureDsJournee, int HeureDemareAenMin, ReservationCreation reservationCreation)
        {
            this.reservationCreation = reservationCreation;
            this.NbHeuresTravaille = NbHeureDsJournee;
            this.HeureDemareAenMin = HeureDemareAenMin;

            this.DynamicGrid = new DynamicGridTools(partialDayOfActivities.Width, false, this.reservationCreation);
            this.View = partialDayOfActivities;
        }
        private string HeureDebut_EnFoncCase(int idCase)
        {
            var r = idCase * granularite;
            var timeInMin = r + HeureDemareAenMin;
            var timeInHours = (int)timeInMin / 60;
            int MinRes = timeInMin % 60;

            string ret = "";

           // if (MinRes == (HeureDemareAenMin % 30))
          //  {
                if (MinRes == 0) { ret = timeInHours.ToString() + "h" + "\n" + this.HeureFin_EnFoncCase(idCase); }
                else { ret = timeInHours.ToString() + "h" + MinRes.ToString() + "Min"; }
           // }
            return ret;

        }
        private string HeureFin_EnFoncCase(int idCase)
        {
            var r = idCase * granularite;
            var timeInMin = r + HeureDemareAenMin + granularite;
            var timeInHours = (int)timeInMin / 60;
            int MinRes = timeInMin % 60;

            string ret = "";

           // if (MinRes == (HeureDemareAenMin % 60))
           // {
                if (MinRes == 0) { ret = timeInHours.ToString() + "h"; }
                else { ret = timeInHours.ToString() + "h" + MinRes.ToString() + "Min"; }
           // }
            return ret;

        }
        public delegate bool isPris();
        public int CurrentIDEquip { get; set; }
        public DateTime Jours { get; set; }
        private void RemplirLaGrille()
        {
            for (var i = 0; i < this.CbIlNousFautDeColones(); i++)
            {
                int debut = this.HeureDemareAenMin + (i*granularite);
                Button button = new Button();
                //button.Content = HeureDebut_EnFoncCase(i);
                if (GestionReservation.isReserved(this.CurrentIDEquip, DateTimeUnixTools.ConvertToUnixTimestamp(this.Jours) + debut))
                { button.Background = Brushes.Green; }
                else
                { button.Background = Brushes.Red; }
                button.Name = "b"+i*granularite;
                button.Click += new RoutedEventHandler(ClickEditHeure);
                button.Margin = new Thickness(0, 0, 0, 0);
                button.ToolTip = HeureDebut_EnFoncCase(i); 
                button.VerticalAlignment = VerticalAlignment.Stretch;
                button.HorizontalAlignment = HorizontalAlignment.Stretch;
                this.DynamicGrid.AddUiToCase(button, 0,i);
            }
        }

        void ClickEditHeure(object sender, RoutedEventArgs e)
        {
                //throw new NotImplementedException();
            string name = ((Button)sender).Name;
            name = name.Substring(1);
            GestionReservation.reservationCreation.heure.Text = ((Int32.Parse(name) + this.HeureDemareAenMin) / 60).ToString();
            GestionReservation.reservationCreation.min.Text = ((Int32.Parse(name) + this.HeureDemareAenMin) % 60).ToString();
            GestionReservation.reservationCreation.valEquipe.Text = this.CurrentIDEquip.ToString();
        }
       
        public List<Label> RemplirLaGrilleLabel()
        {
            List<Label> ret = new List<Label>();
//        <Label Width="200" Height="30" Content="Test" Margin="10,-5,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="Lbl_Titre" FontSize="15" />
            int lataille = 650 / this.CbIlNousFautDeColones();
            for (var i = 0; i < this.CbIlNousFautDeColones(); i++)
            {
                if (HeureDebut_EnFoncCase(i) != String.Empty)
                {
                    Label lab = new Label();
                    lab.Content = lab.Content = HeureDebut_EnFoncCase(i);
                    lab.Margin = new Thickness(i * (lataille + 3), -2, 0, 0);
                 //   lab.Width = 40;
                //    lab.Height = 30;
                    lab.VerticalAlignment = VerticalAlignment.Stretch;
                    lab.HorizontalAlignment = HorizontalAlignment.Stretch;
                //    ret.Add(lab); 
                }

            }
            return ret;
        }
        private int CbIlNousFautDeColones()
        {
            var res = (int)(NbHeuresTravaille * 60);
            var Nb = (int)res / granularite;
            if (res % granularite > 0)
                Nb++;

            return Nb;
        }

        private void CreateNbOfButton() 
        {
            this.DynamicGrid.Reset();
            for (var i = 0; i < this.CbIlNousFautDeColones(); i++)
            {
                int tailleMin = 25;

               // tailleMin = (int)(this.View.RenderSize.Width / this.CbIlNousFautDeColones());
                this.DynamicGrid.CreateColumn(tailleMin);
            }
            this.DynamicGrid.CreateRow(45);
        }
        public DynamicGridTools DynamicGrid { get; set; }


        internal void Render()
        {
            
            
            this.CreateNbOfButton();
            RemplirLaGrille();
            DockPanel DayGranul = this.View;
            DayGranul.Children.Clear();
            DayGranul.Children.Add(
                    this.DynamicGrid.Render());
        }

        public DockPanel View { get; set; }

        

        
    }
}
